package com.openatc.agent.service.impl;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.openatc.agent.service.FeatureService;
import com.openatc.agent.service.GenericMsgStrategy;
import com.openatc.agent.utils.RedisTemplateUtil;
import com.openatc.comm.data.MessageData;
import com.openatc.core.model.RESTRet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.openatc.comm.ocp.CosntDataDefine.getresponse;

/**
 * @Classname GetFeatureAll
 * @Description 处理所有参数的上下载
 * @Date 2022/7/19 16:32
 * @Created by panleilei
 */
@Service(value = "feature/all")
public class FeatureAllRequest extends GenericMsgStrategy {
    @Autowired
    private FeatureService featureService;

    @Autowired
    private RedisTemplateUtil redisTemplateUtil;

    /**
     * 成功上载所有参数
     * @param requestData
     * @param responseData
     * @return
     */
    @Override
    public RESTRet successGetProcess(MessageData requestData, RESTRet responseData) {
        // 将参数保存到数据库
        MessageData res = (MessageData) responseData.getData();

        RESTRet restRet = featureService.saveFeatureAll(res);
        if (!restRet.isSuccess()){
            logger.warning("MsgPostHandler process: save feature failed!");
        }
        return super.successGetProcess(requestData, responseData);
    }

    /**
     * 成功下载所有参数
     * @param requestData
     * @param responseData
     * @return
     */
    @Override
    public RESTRet successSetProcess(MessageData requestData, RESTRet responseData) {
        // 将参数保存到数据库
        RESTRet ret = featureService.saveFeatureAll(requestData);
        if (!ret.isSuccess()){
            logger.warning("MsgPostHandler process: save feature failed!");
        }
        // 更新参数的缓存
        updateCache(requestData);
        return super.successSetProcess(requestData, responseData);
    }



    /**
     * 成功下载全部参数后，更新参数的缓存
     * @param request
     */
    private void updateCache(MessageData request) {
        // 1、从所有参数中分离出单独参数
        Map<String, String> map = getRelect();
        List<MessageData> dataList = getDataList(request,map);
        Long expire = 0L;
        // 2、批量更新参数的缓存
        redisTemplateUtil.mulitSavaMessageData(dataList,expire);
    }

    /**
     * 所有参数中数组名和infotyoe的映射
     * @return
     */
    private Map<String, String> getRelect() {
        Map<String,String> map = new HashMap<>();
        // 相位参数
        String phase = "phaseList";
        String phaseInfotype = "feature/phase";
        // 方案参数
        String pattern = "patternList";
        String patternInfotype = "feature/pattern";

        map.put(phase,phaseInfotype);
        map.put(pattern,patternInfotype);

        return map;
    }

    /**
     * 获取分离出的单独参数
     * @param requestData
     * @Param paramNames
     * @return
     */
    private List<MessageData> getDataList(MessageData requestData,Map<String,String> map) {
        List<MessageData> dataList = new ArrayList<>();
        for (String paramName : map.keySet()) {
            JsonElement jsonElement = requestData.getData().getAsJsonObject().get(paramName);
            MessageData tempData = new MessageData(requestData.getAgentid(),getresponse,map.get(paramName));
            tempData.setCreatetime(requestData.getCreatetime());
            JsonObject jsonObject = new JsonObject();
            jsonObject.add(paramName,jsonElement);
            tempData.setData(jsonObject);
            dataList.add(tempData);
        }
        return dataList;
    }




}
